<template>
  <div class="detail-header">
    <img :src="info.product_pic_url" alt="" />
    <div class="detail-header-right">
      <!-- 标题 -->
      <div class="detail-title">
        <h1>{{ info.title }}</h1>
        <div class="detail-collection" v-if="!isOwner">
          <el-button
            type="danger"
            size="mini"
            :class="{ collectioned: info.isCollection }"
            @click="toggleCollection"
          >
            {{ info.isCollection ? "已收藏" : "收藏" }}
          </el-button>
        </div>
      </div>
      <!-- 属性 -->
      <div class="detail-property">
        <div v-for="item in info.properties_show" :key="item.type">
          <b>{{ item.parent_name }}</b>
          <span>{{ item.name }}</span>
        </div>
      </div>
      <!-- 作者信息 -->
      <div class="user">
        <!-- 头像 -->
        <router-link
          class="avatar"
          :to="{ name: 'user-space', query: { userId: info.userInfo.userId } }"
        >
          <img :src="info.userInfo.avatar" />
        </router-link>
        <!-- 作者信息 -->
        <div class="user-info">
          <h4>
            <router-link
              :to="{
                name: 'user-space',
                query: { userId: info.userInfo.userId },
              }"
            >
              {{ info.userInfo.name }}
            </router-link>
          </h4>
          <div>
            <span>菜谱：{{ info.userInfo.work_menus_len }}</span>
            &nbsp;/&nbsp;
            <span>关注：{{ info.userInfo.following_len }}</span>
            &nbsp;/&nbsp;
            <span>粉丝：{{ info.userInfo.follows_len }}</span>
          </div>
          <div class="create-time">
            <strong>发布时间：{{ info.createdAt.substr(0, 10) }}</strong>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { toggleCollection } from "@/apis/menu.js";
export default {
  props: {
    info: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      // isOwner: false,
      // this.isOwner = !userId || userId == this.$store.state.userInfo.userId;
    };
  },
  computed:{
    isOwner(){
      return this.info.userInfo.userId==this.$store.state.userInfo.userId;
    }
  },
  methods: {
    // 点击按钮，切换收藏与否
    async toggleCollection() {
      // 先判断一下是否登录
      if (!this.$store.getters.isLogin) {
        // 未登录
        this.$confirm("你还没有登录，请先登录", "提示", {
          confirmButtonText: "登录",
          cancelButtonText: "不登录",
        })
          .then((res) => {
            console.log(res);
            // 点击 登录 跳转
            this.$router.push({ name: "login" });
          })
          .catch((err) => {
            console.log(err);
            this.$alert("不登录不能收藏", "提示");
          });
      }
      // 已登录
      const data = await toggleCollection({ menuId: this.info.menuId });
      console.log(data);
      this.info.isCollection = data.data.isCollection;
    },
  },
};
</script>

<style lang="stylus" scoped>
.detail-header {
  background-color: #fff;
  display: flex;

  .detail-header-right {
    margin-left: 20px;
    // flex: 1 1 auto;
    flex-grow: 1;

    .detail-title {
      line-height: 60px;
      border-bottom: 1px solid #888;
      display: flex;
      justify-content: space-between;

      h1 {
        font-size: 24px;
      }

      .detail-collection {
        margin-right: 40px;

        .el-button--danger.collectioned {
          background-color: #888;
          border-color: #666;
        }
      }
    }

    .detail-property {
      display: flex;
      justify-content: space-around;
      border-bottom: 1px solid #888;
      height: 80px;

      div {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        border-right: 1px solid #888;
        align-items: center;

        &:last-child {
          border-right: 0;
        }

        b {
          font-size: 12px;
          color: #666;
        }

        span {
          font-size: 18px;
          color: red;
          font-weight: bold;
        }
      }
    }

    .user {
      margin-top: 30px;
      display: flex;
      align-items: center;

      .avatar {
        img {
          width: 80px;
          border-radius: 50%;
        }
      }

      .user-info {
        flex: 1;
        margin-left: 20px;

        a {
          font-size: 14px;
          color: red;
          line-height: 1.5em;
        }

        span {
          font-size: 12px;
          color: #666;
        }

        b {
          font-size: 12px;
          color: #666;
        }

        strong {
          color: #999;
        }
      }
    }
  }
}
</style>